package com.ofa.dao;

import java.util.List;

import org.springframework.stereotype.Component;

import com.ofa.base.HibernateDAOImpl;
import com.ofa.base.Pagination;
import com.ofa.entity.Organization;
import com.ofa.entity.Person;


@Component(value="personDao")
public class PersonDao extends HibernateDAOImpl<Person> {

	public void save(Person person, Integer orgId) {
		if(orgId != null){
			person.setOrg((Organization)this.getHibernateTemplate().get(Organization.class, orgId));
		}
		getHibernateTemplate().save(person);
	}

	public void update(Person person, int orgId) {
		if(orgId != 0){
			person.setOrg((Organization)this.getHibernateTemplate().load(Organization.class, orgId));
		}
		getHibernateTemplate().update(person);
	}
	

	public Pagination<Person> queryPersons(Person person, int firstResult, int maxResults){
		String hql = "select p from Person p where 1=1 ";
		
		List<Person> list = this.getSession()
				.createQuery(hql)
				.setFirstResult(firstResult)
				.setMaxResults(maxResults)
				.list();

		String countHql = "select count(p.id) from Person p where 1=1 ";
		int total = ((Long)(this.getSession().createQuery(countHql).uniqueResult())).intValue();
		
		Pagination<Person> pagination = new Pagination<Person>();
		pagination.setData(list);
		pagination.setTotal(total);
		return pagination;
	}
	
	
	
	
	
}
